package com.blockchain.securewallet.activities;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.blockchain.securewallet.MainActivity;
import com.blockchain.securewallet.R;
import com.blockchain.securewallet.WalletApp;
import com.blockchain.securewallet.base.BaseActivity;
import com.blockchain.securewallet.tools.ActivityLauncher;
import com.blockchain.securewallet.utils.StringUtil;
import com.blockchain.securewallet.utils.ToastUtils;
import com.blockchain.securewallet.utils.UIHelper;
import com.google.zxing.common.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UnLockWalletActivity extends BaseActivity implements TextWatcher {

    @BindView(R.id.btn_back)
    ImageView ivBack;

    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.tv_tips_pw_err)
    TextView tv_tips_pw_err;

    @BindView(R.id.tv_unlock_other)
    TextView tv_unlock_other;
    
    @BindView(R.id.tv_forget_password)
    TextView tv_forget_password;

    @BindView(R.id.edit_pay_password)
    EditText payPassword;

    public static final String FROM = "LOCK";
    private String getFrom;
    //是否设置了手势密码
    private boolean isGestureEntrance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wallet_unlock);
        ButterKnife.bind(this);
        tvTitle.setText("解锁钱包");
        ivBack.setVisibility(View.GONE);

        getFrom = getIntent().getStringExtra("from");

        payPassword.addTextChangedListener(this);

        UIHelper.expandViewTouchDelegate(tv_unlock_other, 60, 30, 60, 60);
        UIHelper.expandViewTouchDelegate(tv_forget_password, 10, 30, 30, 10);

        boolean lockSet = WalletApp.getInstance().isLockPasswordSet();
        //手势密码开 且 手势密码不为空
        isGestureEntrance = lockSet && StringUtil.noEmpty(WalletApp.getInstance().getLockPassword());

        //显示其他解锁方式
        if (isGestureEntrance) {
            tv_unlock_other.setVisibility(View.VISIBLE);
        }
    }

    
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        getFrom = getIntent().getStringExtra("from");
    }


    @OnClick(R.id.btn_wallet_unlock)
    void unlock() {
        String password = WalletApp.getInstance().getPayPassword();
        String now = payPassword.getText().toString();
        if (StringUtil.isEmpty(now)) {
            tv_tips_pw_err.setText(StringUtil.getResourceString(R.string.tips_lock_password_null));
            tv_tips_pw_err.setVisibility(View.VISIBLE);
        } else if (password.equals(now)) {
            MainActivity.startFromWalletCreate(UnLockWalletActivity.this);
        } else {
            //Toast.makeText(this,"密码不正确",Toast.LENGTH_SHORT).show();
            tv_tips_pw_err.setText(StringUtil.getResourceString(R.string.tips_lock_password_unlock_error));
            tv_tips_pw_err.setVisibility(View.VISIBLE);
        }
    }


    @OnClick(R.id.tv_forget_password)
    void forget() {
        //可以返回
        ActivityLauncher.gotoWalletInitActivity(this, false);
    }
    
    @OnClick(R.id.tv_unlock_other)
    void unlock_other() {
        gotoUnlockActivity();
    }


    //前往手势解锁界面
    private void gotoUnlockActivity() {
        //这里需要注意一种情况，就是还没有设置手势密码，就不能直接进行跳转
        //是否设置了手势密码，true为是
        if (isGestureEntrance) {
            finish();
        }
    }



    @Override
    public void onBackPressed() {

        if (getFrom != null && getFrom.equals(FROM)) {
            super.onBackPressed();
        } else {

        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        //如果显示了，就隐藏掉；如果隐藏了，就别管。
        if (tv_tips_pw_err.getVisibility() == View.VISIBLE) {
            tv_tips_pw_err.setVisibility(View.GONE);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }


}
